package com.yzh.booksystem.controller;

import com.yzh.booksystem.entity.Book;
import com.yzh.booksystem.service.AddBookService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "AddBookServlet", urlPatterns = "/AddBookServlet")
public class AddBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求编码和响应内容的编码转换
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        int id = Integer.parseInt(request.getParameter("id"));
        String bookname = request.getParameter("bookname");
        float price = Float.parseFloat(request.getParameter("price"));
        String address = request.getParameter("address");
        String isbn = request.getParameter("isbn");
        Book book = new Book();
        book.setId(id);
        book.setBookname(bookname);
        book.setPrice(price);
        book.setAddress(address);
        book.setIsbn(isbn);


        AddBookService addBookService = new AddBookService();
        HttpSession session = request.getSession();
        try {
            if (addBookService.checkAddBook(book)) {
                if (addBookService.total_books_show() != null) {
//                    List<Book> listBooks = addBookService.total_books_show();
//                    session.setAttribute("listBooks", listBooks);//原来问题在这，listBooks传输过去怎么少一个s
//                    response.setHeader("refresh", "3;url=/booksystem/index.jsp");
                    //response.sendRedirect("/booksystem/index.jsp");
                    List<Book> listBooks = addBookService.total_books_show();
                    request.setAttribute("listBooks", listBooks);
                    request.getRequestDispatcher("/index.jsp").forward(request, response);
//                    session.setAttribute("listBooks", listBooks);
//                    response.setHeader("refresh", "3;url=/booksystem/index.jsp");
                }
} else {

                request.setAttribute("error", "该书已存在，添加失败");
                request.getRequestDispatcher("/addbook.jsp").forward(request, response);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

        /*校验登录用户时可以用到，此处先不用
        switch (i) {
            case 0:
                request.setAttribute("error0", "id错误，请重新输入");
                String error = (String) request.getAttribute("error1");
            case 1:
                return "error1";
            case 2:
                return "error2";
            case 3:
                return "error3";
            case 4:
                return "error4";
        }
        */

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
